import { isReallyNull } from '../common/util'
import { storages } from '../storage'
import { baseURL } from './config'

const axiosFilter = {
  reqHandleFunc: {
    baseURL: baseURL,
    headers: {
      'Content-Type': 'application/json',
    },
    timeout: 3000,
    withCredentials: false,
  },
  reqErrorFunc: () => {},
  resHandleFunc: () => {},
  resErrorFunc: () => {},
  create(filter, data) {
    switch (filter) {
    case 'reqHandleFunc': {
      let config = { ...this[filter], ...data }
      let token = storages.token.get()
      if (!isReallyNull(token)) {
        config.headers['x-auth-token'] = token
      }
      return config
    }
    case 'reqErrorFunc':
      return Promise.reject(data)
    case 'resHandleFunc':
      return data
    case 'resErrorFunc':
      return Promise.reject(data)
    default:
      return null
    }
  },
}

export { axiosFilter }
